<!-- build time:Wed Dec 30 2020 09:55:48 GMT+0800 (中国标准时间) --><!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=2"><meta name="theme-color" content="#222"><meta name="generator" content="Hexo 4.2.1"><link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png"><link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png"><link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png"><link rel="mask-icon" href="/images/logo.svg" color="#222"><link rel="stylesheet" href="/css/main.css"><link rel="stylesheet" href="/lib/font-awesome/css/all.min.css"><link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css"><script id="hexo-configurations">var NexT=window.NexT||{},CONFIG={hostname:"allens.icu",root:"/",scheme:"Pisces",version:"7.8.0",exturl:!1,sidebar:{position:"left",display:"post",padding:18,offset:12,onmobile:!1},copycode:{enable:!0,show_result:!0,style:"mac"},back2top:{enable:!0,sidebar:!0,scrollpercent:!0},bookmark:{enable:!1,color:"#222",save:"auto"},fancybox:!0,mediumzoom:!1,lazyload:!0,pangu:!1,comments:{style:"tabs",active:null,storage:!0,lazyload:!1,nav:null},algolia:{hits:{per_page:10},labels:{input_placeholder:"Search for Posts",hits_empty:"We didn't find any results for the search: ${query}",hits_stats:"${hits} results found in ${time} ms"}},localsearch:{enable:!0,trigger:"auto",top_n_per_article:1,unescape:!1,preload:!1},motion:{enable:!0,async:!1,transition:{post_block:"fadeIn",post_header:"slideDownIn",post_body:"slideDownIn",coll_header:"slideLeftIn",sidebar:"slideUpIn"}},path:"search.xml"}</script><meta name="description" content="基于微信XLog的日志框架&amp;&amp;对于XLog的分析"><meta property="og:type" content="article"><meta property="og:title" content="基于微信XLog的日志框架&amp;&amp;对于XLog的分析"><meta property="og:url" content="https://allens.icu/posts/60625924/index.html"><meta property="og:site_name" content="在雨季等你"><meta property="og:description" content="基于微信XLog的日志框架&amp;&amp;对于XLog的分析"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://gitee.com/_Allens/BlogImage/raw/master/image/y9lop.png"><meta property="og:image" content="https://gitee.com/_Allens/BlogImage/raw/master/image/x381l.png"><meta property="og:image" content="https://www.jitpack.io/v/JiangHaiYang01/XLogHelper.svg"><meta property="article:published_time" content="2020-07-16T09:37:59.000Z"><meta property="article:modified_time" content="2020-10-27T11:44:41.734Z"><meta property="article:author" content="在雨季等你"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://gitee.com/_Allens/BlogImage/raw/master/image/y9lop.png"><link rel="canonical" href="https://allens.icu/posts/60625924/"><script id="page-configurations">CONFIG.page={sidebar:"",isHome:!1,isPost:!0,lang:"zh-CN"}</script><title>基于微信XLog的日志框架&&对于XLog的分析 | 在雨季等你</title><noscript><style>.sidebar-inner,.use-motion .brand,.use-motion .collection-header,.use-motion .comments,.use-motion .menu-item,.use-motion .pagination,.use-motion .post-block,.use-motion .post-body,.use-motion .post-header{opacity:initial}.use-motion .site-subtitle,.use-motion .site-title{opacity:initial;top:initial}.use-motion .logo-line-before i{left:initial}.use-motion .logo-line-after i{right:initial}</style></noscript></head><body itemscope itemtype="http://schema.org/WebPage"><div class="container use-motion"><div class="headband"></div><header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-container"><div class="site-nav-toggle"><div class="toggle" aria-label="切换导航栏"><span class="toggle-line toggle-line-first"></span> <span class="toggle-line toggle-line-middle"></span> <span class="toggle-line toggle-line-last"></span></div></div><div class="site-meta"><a href="/" class="brand" rel="start"><span class="logo-line-before"><i></i></span><h1 class="site-title">在雨季等你</h1><span class="logo-line-after"><i></i></span></a></div><div class="site-nav-right"><div class="toggle popup-trigger"><i class="fa fa-search fa-fw fa-lg"></i></div></div></div><nav class="site-nav"><ul id="menu" class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="tags fa-fw"></i>标签<span class="badge">6</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="th fa-fw"></i>分类<span class="badge">7</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="archive fa-fw"></i>归档<span class="badge">23</span></a></li><li class="menu-item menu-item-search"><a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索</a></li></ul></nav><div class="search-pop-overlay"><div class="popup search-popup"><div class="search-header"><span class="search-icon"><i class="fa fa-search"></i></span><div class="search-input-container"><input autocomplete="off" autocapitalize="off" placeholder="搜索..." spellcheck="false" type="search" class="search-input"></div><span class="popup-btn-close"><i class="fa fa-times-circle"></i></span></div><div id="search-result"><div id="no-result"><i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i></div></div></div></div></div></header><main class="main"><div class="main-inner"><div class="content-wrap"><div class="content post posts-expand"><article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN"><link itemprop="mainEntityOfPage" href="https://allens.icu/posts/60625924/"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/images/heard.jpg"><meta itemprop="name" content="在雨季等你"><meta itemprop="description" content="分享是进步最快的方式"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="在雨季等你"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">基于微信XLog的日志框架&&对于XLog的分析</h1><div class="post-meta"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建时间：2020-07-16 17:37:59" itemprop="dateCreated datePublished" datetime="2020-07-16T17:37:59+08:00">2020-07-16</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar-check"></i> </span><span class="post-meta-item-text">更新于</span> <time title="修改时间：2020-10-27 19:44:41" itemprop="dateModified" datetime="2020-10-27T19:44:41+08:00">2020-10-27</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-folder"></i> </span><span class="post-meta-item-text">分类于</span> <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a> </span>， <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Android/%E5%BC%80%E6%BA%90/" itemprop="url" rel="index"><span itemprop="name">开源</span></a> </span></span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-comment"></i> </span><span class="post-meta-item-text">评论数：</span> <a title="valine" href="/posts/60625924/#valine-comments" itemprop="discussionUrl"><span class="post-comments-count valine-comment-count" data-xid="/posts/60625924/" itemprop="commentCount"></span></a></span><br><span class="post-meta-item" title="本文字数"><span class="post-meta-item-icon"><i class="far fa-file-word"></i> </span><span class="post-meta-item-text">本文字数：</span> <span>17k</span> </span><span class="post-meta-item" title="阅读时长"><span class="post-meta-item-icon"><i class="far fa-clock"></i> </span><span class="post-meta-item-text">阅读时长 &asymp;</span> <span>15 分钟</span></span></div></header><div class="post-body" itemprop="articleBody"><p>基于微信XLog的日志框架&amp;&amp;对于XLog的分析</p><a id="more"></a><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>之前写过一个 日志框架<a href="https://github.com/JiangHaiYang01/LogHelper" target="_blank" rel="noopener">LogHelper</a> ,是基于 Logger 开源库封装的，当时的因为项目本身的日志不是很多，完全可以使用，最近和其他公司合作，在一个新的项目上反馈，说在 大量log 的情况下会影响到手机主体功能的使用。从而让我对之前的日志行为做了一个深刻的反省</p><p>随后在开发群中咨询了其他开发的小伙伴，如果追求性能，可以研究一下 微信的 xlog ,也是本篇博客的重点</p><h1 id="xlog-是什么"><a href="#xlog-是什么" class="headerlink" title="xlog 是什么"></a>xlog 是什么</h1><p>xlog 是什么 这个问题 我这也是在<a href="https://zhuanlan.zhihu.com/p/23879436" target="_blank" rel="noopener">【腾讯Bugly干货分享】微信mars 的高性能日志模块 xlog</a>得到了答案</p><p>简单来说 ，就是腾讯团队分享的基于 c/c++ 高可靠性高性能的运行期日志组件</p><h1 id="官网的-sample"><a href="#官网的-sample" class="headerlink" title="官网的 sample"></a>官网的 sample</h1><p>知道了他是什么，就要只要他是怎么用的，打开github 找到官网<a href="https://github.com/Tencent/mars" target="_blank" rel="noopener">Tencent/mars</a></p><p>使用非常简单</p><h2 id="下载库"><a href="#下载库" class="headerlink" title="下载库"></a>下载库</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">dependencies &#123;</span><br><span class="line">    compile &#39;com.tencent.mars:mars-xlog:1.2.3&#39;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">System.loadLibrary(&quot;c++_shared&quot;);</span><br><span class="line">System.loadLibrary(&quot;marsxlog&quot;);</span><br><span class="line"></span><br><span class="line">final String SDCARD &#x3D; Environment.getExternalStorageDirectory().getAbsolutePath();</span><br><span class="line">final String logPath &#x3D; SDCARD + &quot;&#x2F;marssample&#x2F;log&quot;;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; this is necessary, or may crash for SIGBUS</span><br><span class="line">final String cachePath &#x3D; this.getFilesDir() + &quot;&#x2F;xlog&quot;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;init xlog</span><br><span class="line">if (BuildConfig.DEBUG) &#123;</span><br><span class="line">    Xlog.appenderOpen(Xlog.LEVEL_DEBUG, Xlog.AppenderModeAsync, cachePath, logPath, &quot;MarsSample&quot;, 0, &quot;&quot;);</span><br><span class="line">    Xlog.setConsoleLogOpen(true);</span><br><span class="line"></span><br><span class="line">&#125; else &#123;</span><br><span class="line">    Xlog.appenderOpen(Xlog.LEVEL_INFO, Xlog.AppenderModeAsync, cachePath, logPath, &quot;MarsSample&quot;, 0, &quot;&quot;);</span><br><span class="line">    Xlog.setConsoleLogOpen(false);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Log.setLogImp(new Xlog());</span><br></pre></td></tr></table></figure><p>OK 实现了他的功能</p><blockquote><p>不要高兴的太早，后续的问题都头大</p></blockquote><h1 id="分析各个方法的作用"><a href="#分析各个方法的作用" class="headerlink" title="分析各个方法的作用"></a>分析各个方法的作用</h1><p>知道了最简单的用法，就想看看他支持哪些功能</p><p>按照官网的demo 首先分析一下<code>appenderOpen</code></p><h2 id="appenderOpen-int-level-int-mode-String-cacheDir-String-logDir-String-nameprefix-int-cacheDays-String-pubkey"><a href="#appenderOpen-int-level-int-mode-String-cacheDir-String-logDir-String-nameprefix-int-cacheDays-String-pubkey" class="headerlink" title="appenderOpen(int level, int mode, String cacheDir, String logDir, String nameprefix, int cacheDays, String pubkey)"></a>appenderOpen(int level, int mode, String cacheDir, String logDir, String nameprefix, int cacheDays, String pubkey)</h2><h3 id="level"><a href="#level" class="headerlink" title="level"></a>level</h3><p>日志级别 没啥好说的 XLog 中已经写得很清楚了</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">public static final int LEVEL_ALL &#x3D; 0;</span><br><span class="line">public static final int LEVEL_VERBOSE &#x3D; 0;</span><br><span class="line">public static final int LEVEL_DEBUG &#x3D; 1;</span><br><span class="line">public static final int LEVEL_INFO &#x3D; 2;</span><br><span class="line">public static final int LEVEL_WARNING &#x3D; 3;</span><br><span class="line">public static final int LEVEL_ERROR &#x3D; 4;</span><br><span class="line">public static final int LEVEL_FATAL &#x3D; 5;</span><br><span class="line">public static final int LEVEL_NONE &#x3D; 6;</span><br></pre></td></tr></table></figure><blockquote><p>值得注意的地方 debug 版本下建议把控制台日志打开，日志级别设为 Verbose 或者 Debug, release 版本建议把控制台日志关闭，日志级别使用 Info.</p></blockquote><p>这个在官网的 <a href="https://github.com/Tencent/mars/wiki/Mars-Android-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97" target="_blank" rel="noopener">接入指南</a></p><p>这里也可以使用</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">native</span> <span class="keyword">void</span> <span class="title">setLogLevel</span><span class="params">(<span class="keyword">int</span> logLevel)</span></span>;</span><br></pre></td></tr></table></figure><p>方法设置</p><h3 id="mode"><a href="#mode" class="headerlink" title="mode"></a>mode</h3><p>写入的模式</p><ul><li>public static final int AppednerModeAsync = 0;</li></ul><p>异步写入</p><ul><li>public static final int AppednerModeSync = 1;</li></ul><p>同步写入</p><p>同步写入，可以理解为实时的日志，异步则不是</p><blockquote><p>Release版本一定要用 AppednerModeAsync， Debug 版本两个都可以，但是使用 AppednerModeSync 可能会有卡顿</p></blockquote><p>这里也可以使用</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">native</span> <span class="keyword">void</span> <span class="title">setAppenderMode</span><span class="params">(<span class="keyword">int</span> mode)</span></span>;</span><br></pre></td></tr></table></figure><p>方法设置</p><h3 id="cacheDir-设置缓存目录"><a href="#cacheDir-设置缓存目录" class="headerlink" title="cacheDir 设置缓存目录"></a>cacheDir 设置缓存目录</h3><p>缓存目录，当 logDir 不可写时候会写进这个目录，可选项，不选用请给 “”， 如若要给，建议给应用的 /data/data/packname/files/log 目录。</p><p>会在目录下生成后缀为 .mmap3 的缓存文件，</p><h3 id="logDir-设置写入的文件目录"><a href="#logDir-设置写入的文件目录" class="headerlink" title="logDir 设置写入的文件目录"></a>logDir 设置写入的文件目录</h3><p>真正的日志，后缀为 .xlog</p><blockquote><p>日志写入目录，请给单独的目录，除了日志文件不要把其他文件放入该目录，不然可能会被日志的自动清理功能清理掉。</p></blockquote><h3 id="nameprefix-设置日志文件名的前缀"><a href="#nameprefix-设置日志文件名的前缀" class="headerlink" title="nameprefix 设置日志文件名的前缀"></a>nameprefix 设置日志文件名的前缀</h3><p>日志文件名的前缀，例如该值为TEST，生成的文件名为：TEST_20170102.xlog。</p><h3 id="cacheDays"><a href="#cacheDays" class="headerlink" title="cacheDays"></a>cacheDays</h3><p>一般情况下填0即可。非0表示会在 _cachedir 目录下存放几天的日志。</p><p>这里的描述比较晦涩难懂，当我设置这个参数非0 的时候 会发现 原本设置在 logDir 目录下的文件 出现在了 cacheDir</p><p>例如 正常应该是</p><p>文件结构</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">- cacheDir</span><br><span class="line">    - log.mmap3</span><br><span class="line">- logDir</span><br><span class="line">    - log_20200710.xlog</span><br><span class="line">    - log_20200711.xlog</span><br></pre></td></tr></table></figure><p>变成这样</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">- cacheDir</span><br><span class="line">    - log.mmap3</span><br><span class="line">    - log_20200710.xlog</span><br><span class="line">    - log_20200711.xlog</span><br><span class="line">- logDir</span><br></pre></td></tr></table></figure><p>全部到了 cacheDir 下面</p><blockquote><p>cacheDays 的意思是 在多少天以后 从缓存目录移到日志目录</p></blockquote><h3 id="pubkey-设置加密的-pubkey"><a href="#pubkey-设置加密的-pubkey" class="headerlink" title="pubkey 设置加密的 pubkey"></a>pubkey 设置加密的 pubkey</h3><p>这里涉及到了日志的加密与解密，下面会专门介绍</p><h2 id="setMaxFileSize-设置文件大小"><a href="#setMaxFileSize-设置文件大小" class="headerlink" title="setMaxFileSize 设置文件大小"></a>setMaxFileSize 设置文件大小</h2><p>在 Xlog 下有一个 native 方法</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">public static native void setMaxFileSize(long size);</span><br></pre></td></tr></table></figure><p>他表示 最大文件大小，这里需要说一下，原本的默认设置 是一天一个日志文件在 <a href="https://github.com/Tencent/mars/blob/master/mars/log/appender.h" target="_blank" rel="noopener">appender.h</a> 描述的很清楚</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * By default, all logs will write to one file everyday. You can split logs to multi-file by changing max_file_size.</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * @param _max_byte_size    Max byte size of single log file, default is 0, meaning do not split.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">appender_set_max_file_size</span><span class="params">(uint64_t _max_byte_size)</span></span>;</span><br></pre></td></tr></table></figure><p>默认情况下，所有日志每天都写入一个文件。可以通过更改max_file_size将日志分割为多个文件。单个日志文件的最大字节大小，默认为0，表示不分割</p><p>当超过设置的文件大小以后。文件会变成如下目录结构</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">- cacheDir</span><br><span class="line">    - log.mmap3</span><br><span class="line">- logDir</span><br><span class="line">    - log_20200710.xlog</span><br><span class="line">    - log_20200710_1.xlog</span><br><span class="line">    - log_20200710_2.xlog</span><br></pre></td></tr></table></figure><p>在 <a href="https://github.com/Tencent/mars/blob/master/mars/log/src/appender.cc" target="_blank" rel="noopener">appender.cc</a> 对应的有如下逻辑，</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">static long __get_next_fileindex(const std::string&amp; _fileprefix, const std::string&amp; _fileext) &#123;</span><br><span class="line">    ...</span><br><span class="line">    return (filesize &gt; sg_max_file_size) ? index + 1 : index;</span><br></pre></td></tr></table></figure><h2 id="setConsoleLogOpen-设置是否在控制台答应日志"><a href="#setConsoleLogOpen-设置是否在控制台答应日志" class="headerlink" title="setConsoleLogOpen  设置是否在控制台答应日志"></a>setConsoleLogOpen 设置是否在控制台答应日志</h2><p>···java<br>public static native void setConsoleLogOpen(boolean isOpen);<br>···</p><p>设置是否在控制台答应日志</p><h2 id="setErrLogOpen"><a href="#setErrLogOpen" class="headerlink" title="setErrLogOpen"></a>setErrLogOpen</h2><p>这个方法是没用的，一开始以为哪里继承的有问题，在查看源码的时候发现 他是一个空方法，没有应用</p><p><img data-src="https://gitee.com/_Allens/BlogImage/raw/master/image/y9lop.png" alt=""></p><p>使用的话会导致程序异常,在自己编译的so 中我就把它给去掉了</p><h2 id="setMaxAliveTime-设置单个文件最大保留时间"><a href="#setMaxAliveTime-设置单个文件最大保留时间" class="headerlink" title="setMaxAliveTime 设置单个文件最大保留时间"></a>setMaxAliveTime 设置单个文件最大保留时间</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">native</span> <span class="keyword">void</span> <span class="title">setMaxAliveTime</span><span class="params">(<span class="keyword">long</span> duration)</span></span>;</span><br></pre></td></tr></table></figure><p>置单个文件最大保留时间 单位是秒，这个方法有3个需要注意的地方，</p><ul><li>必须在 appenderOpen 方法之前才有效</li><li>最小的时间是 一天</li><li>默认的时间是10天</li></ul><p>在 <a href="https://github.com/Tencent/mars/blob/master/mars/log/src/appender.cc" target="_blank" rel="noopener">appender.cc</a> 中可以看到</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">static const long kMaxLogAliveTime &#x3D; 10 * 24 * 60 * 60;    &#x2F;&#x2F; 10 days in second</span><br><span class="line">static const long kMinLogAliveTime &#x3D; 24 * 60 * 60;    &#x2F;&#x2F; 1 days in second</span><br><span class="line">static long sg_max_alive_time &#x3D; kMaxLogAliveTime;</span><br><span class="line"></span><br><span class="line">....</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">void appender_set_max_alive_duration(long _max_time) &#123;</span><br><span class="line">	if (_max_time &gt;&#x3D; kMinLogAliveTime) &#123;</span><br><span class="line">		sg_max_alive_time &#x3D; _max_time;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>默认的时间是10天</p><h2 id="appenderClose"><a href="#appenderClose" class="headerlink" title="appenderClose"></a>appenderClose</h2><p>在 <a href="https://github.com/Tencent/mars#mars_cn" target="_blank" rel="noopener">文档</a>中介绍说是在 程序退出时关闭日志 调用appenderClose的方法</p><p>然而在实际情况中 Application 类的 onTerminate() 只有在模拟器中才会生效，在真机中无效的，</p><p>如果在程序退出的时候没有触发 appenderClose 那么在下一次启动的时候，xlog 也会把日志写入到文件中</p><p>所以如何触发呢？</p><p>建议尽可能的去触发他 例如用户双击back 退出的情况下 你肯定是知道的<br>如果放在后台被杀死了，这个时候也真的没办法刷新，也没关系，上面也说了，再次启动的时候会刷新到日志中，</p><h2 id="appenderFlush"><a href="#appenderFlush" class="headerlink" title="appenderFlush"></a>appenderFlush</h2><p>当日志写入模式为异步时，调用该接口会把内存中的日志写入到文件。</p><p>isSync : true 为同步 flush，flush 结束后才会返回。<br>isSync : false 为异步 flush，不等待 flush 结束就返回。</p><h1 id="日志文件的加密"><a href="#日志文件的加密" class="headerlink" title="日志文件的加密"></a>日志文件的加密</h1><p>这一块单独拿出来说明，是因为之前使用上遇到了坑</p><p>首先是这个 入参 PUB_KEY,一脸懵，是个啥，</p><p>在 <a href="https://github.com/Tencent/mars/blob/master/mars/log/crypt/gen_key.py" target="_blank" rel="noopener">mars/blob/master/mars/log/crypt/gen_key.py</a> 这个就是能够获取到 PUB_KEY 的方法</p><p>运行如下</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$ python gen_key.py</span><br><span class="line">WARNING: Executing a script that <span class="keyword">is</span> loading libcrypto <span class="keyword">in</span> an unsafe way. This will fail <span class="keyword">in</span> a future version of macOS. Set the LIBRESSL_REDIRECT_STUB_ABORT=<span class="number">1</span> <span class="keyword">in</span> the environment to force this into an error.</span><br><span class="line">save private key</span><br><span class="line"><span class="number">471e607</span>b1bb3760205f74a5e53d2764f795601e241ebc780c849e7fde1b4ce40</span><br><span class="line"></span><br><span class="line">appender_open<span class="string">'s parameter:</span></span><br><span class="line"><span class="string">300330b09d9e771d6163bc53a4e23b188ac9b2f5c7150366835bce3a12b0c8d9c5ecb0b15274f12b2dffae7f4b11c3b3d340e0521e8690578f51813c93190e1e</span></span><br></pre></td></tr></table></figure><p>上面的 private key 自己保存好</p><p>appender_open’s parameter: 就是需要的 PUB_KEY</p><h1 id="日志文件的解密"><a href="#日志文件的解密" class="headerlink" title="日志文件的解密"></a>日志文件的解密</h1><p>上面已经知道如何加密了，现在了解一下如何解密</p><h2 id="下载pyelliptic1"><a href="#下载pyelliptic1" class="headerlink" title="下载pyelliptic1"></a>下载pyelliptic1</h2><p>在<a href="https://github.com/Tencent/mars/wiki/Xlog-%E5%8A%A0%E5%AF%86%E4%BD%BF%E7%94%A8%E6%8C%87%E5%BC%95" target="_blank" rel="noopener">Xlog 加密使用指引</a>中能够看到</p><p>需要下载 <a href="https://github.com/yann2192/pyelliptic/releases/tag/1.5.7" target="_blank" rel="noopener">pyelliptic1.5.7</a><br>然后编译 否则下面的命令会失败</p><h2 id="直接解密脚本"><a href="#直接解密脚本" class="headerlink" title="直接解密脚本"></a>直接解密脚本</h2><p>xlog 很贴心的给我们提供了两个脚本</p><p>使用 <a href="https://github.com/Tencent/mars/blob/master/mars/log/crypt/decode_nomars_crypt_log_file.py" target="_blank" rel="noopener">decode_mars_nocrypt_log_file.py</a> 解压没有加密的</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python decode_mars_nocrypt_log_file [path]</span><br></pre></td></tr></table></figure><p>使用 <a href="https://github.com/Tencent/mars/blob/master/mars/log/crypt/decode_mars_crypt_log_file.py" target="_blank" rel="noopener">decode_mars_crypt_log_file.py</a> 加密的文件</p><p>在使用之前需要将 脚本中的</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">PRIV_KEY &#x3D; &quot;145aa7717bf9745b91e9569b80bbf1eedaa6cc6cd0e26317d810e35710f44cf8&quot;</span><br><span class="line">PUB_KEY &#x3D; &quot;572d1e2710ae5fbca54c76a382fdd44050b3a675cb2bf39feebe85ef63d947aff0fa4943f1112e8b6af34bebebbaefa1a0aae055d9259b89a1858f7cc9af9df1&quot;</span><br></pre></td></tr></table></figure><p>改成上面自己获取到的 key 否则是解压不出来的</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python decode_mars_crypt_log_file.py ~/Desktop/log/log_20200710.xlog</span><br></pre></td></tr></table></figure><p>直接生成一个</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">- cacheDir</span><br><span class="line">    - log.mmap3</span><br><span class="line">- logDir</span><br><span class="line">    - log_20200710.xlog</span><br><span class="line">    - log_20200710.xlog.log</span><br></pre></td></tr></table></figure><p>也可以自定义名字</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python decode_mars_crypt_log_file.py ~/Desktop/log/log_20200710.xlog ~/Desktop/log/<span class="number">1.l</span>og</span><br></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">- cacheDir</span><br><span class="line">    - log.mmap3</span><br><span class="line">- logDir</span><br><span class="line">    - log_20200710.xlog</span><br><span class="line">    - <span class="number">1</span>.log</span><br></pre></td></tr></table></figure><h1 id="修改日志的格式"><a href="#修改日志的格式" class="headerlink" title="修改日志的格式"></a>修改日志的格式</h1><p>打开我们解压好的日志查看</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">^^^^^^^^^^Oct 14 2019^^^20:27:59^^^^^^^^^^[17223,17223][2020-07-24 +0800 09:49:19]</span><br><span class="line">get mmap time: 3</span><br><span class="line">MARS_URL: </span><br><span class="line">MARS_PATH: master</span><br><span class="line">MARS_REVISION: 85b19f92</span><br><span class="line">MARS_BUILD_TIME: 2019-10-14 20:27:57</span><br><span class="line">MARS_BUILD_JOB: </span><br><span class="line">log appender mode:0, use mmap:1</span><br><span class="line">cache dir space info, capacity:57926635520 free:52452691968 available:52452691968</span><br><span class="line">log dir space info, capacity:57926635520 free:52452691968 available:52452691968</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.179][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 1</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.180][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 2</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.180][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 3</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.180][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 4</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.181][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 5</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.181][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 6</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.182][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 7</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.182][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 8</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.182][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 9</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.183][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 10</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.183][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 11</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.183][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 12</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.184][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 13</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.184][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 14</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.185][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 15</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.185][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 16</span><br><span class="line">[I][2020-07-24 +8.0 09:49:21.185][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 17</span><br></pre></td></tr></table></figure><p>我擦泪 除了我们需要的信息以外，还有这么多杂七杂八的信息，如何去掉，并且自己定义一下格式</p><p>这里就需要自己去编译 so 了，好在 xlog 已经给我们提供了很好的编译代码</p><p>对应的文档 <a href="https://github.com/Tencent/mars/wiki/Mars-Android-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97#local_compile" target="_blank" rel="noopener">本地编译</a></p><blockquote><p>对于编译这块按照文档来就好了 需要注意的是</p></blockquote><ul><li>一定要用 ndk-r20 不要用最新版本的 21</li><li>一定用 Python2.7 mac 自带 不用要 Python3</li></ul><h2 id="去掉头文件"><a href="#去掉头文件" class="headerlink" title="去掉头文件"></a>去掉头文件</h2><p>首先我们去到这个头文件，对于一个日志框架来着，这个没啥用</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">^^^^^^^^^^Oct 14 2019^^^20:27:59^^^^^^^^^^[17223,17223][2020-07-24 +0800 09:49:19]</span><br><span class="line">get mmap time: 3</span><br><span class="line">MARS_URL: </span><br><span class="line">MARS_PATH: master</span><br><span class="line">MARS_REVISION: 85b19f92</span><br><span class="line">MARS_BUILD_TIME: 2019-10-14 20:27:57</span><br><span class="line">MARS_BUILD_JOB: </span><br><span class="line">log appender mode:0, use mmap:1</span><br><span class="line">cache dir space info, capacity:57926635520 free:52452691968 available:52452691968</span><br><span class="line">log dir space info, capacity:57926635520 free:52452691968 available:52452691968</span><br></pre></td></tr></table></figure><p>在本机下载好的 mars 下，找到 <a href="https://github.com/Tencent/mars/blob/master/mars/log/src/appender.cc" target="_blank" rel="noopener">appender.cc</a> 将头文件去掉</p><p><img data-src="https://gitee.com/_Allens/BlogImage/raw/master/image/x381l.png" alt=""></p><h2 id="修改日志格式"><a href="#修改日志格式" class="headerlink" title="修改日志格式"></a>修改日志格式</h2><p>默认的格式很长</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[I][2020-07-24 +8.0 09:49:21.179][17223, 17223][TAG][, , 0][&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; 1</span><br></pre></td></tr></table></figure><p>[日志级别][时间][pid,tid][tag][filename,strFuncName,line][日志内容</p><p>是一个这样结构</p><p>比较乱，我们想要的日志 就时间，级别，日志内容 就行了</p><p>找到 <a href="https://github.com/Tencent/mars/blob/master/mars/log/src/formater.cc" target="_blank" rel="noopener">formater.cc</a></p><p>将原本的</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> ret = snprintf((<span class="keyword">char</span>*)_log.PosPtr(), <span class="number">1024</span>, <span class="string">"[%s][%s][%"</span> PRIdMAX <span class="string">", %"</span> PRIdMAX <span class="string">"%s][%s][%s, %s, %d]["</span>,  <span class="comment">// **CPPLINT SKIP**</span></span><br><span class="line">                          _logbody ? levelStrings[_info-&gt;level] : levelStrings[kLevelFatal], temp_time,</span><br><span class="line">                          _info-&gt;pid, _info-&gt;tid, _info-&gt;tid == _info-&gt;maintid ? <span class="string">"*"</span> : <span class="string">""</span>, _info-&gt;tag ? _info-&gt;tag : <span class="string">""</span>,</span><br><span class="line">                          filename, strFuncName, _info-&gt;line);</span><br></pre></td></tr></table></figure><p>改成</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> ret = snprintf((<span class="keyword">char</span>*)_log.PosPtr(), <span class="number">1024</span>,     <span class="string">"[%s][%s]"</span>,  <span class="comment">// **CPPLINT SKIP**</span></span><br><span class="line">                        temp_time,   _logbody ? levelStrings[_info-&gt;level] : levelStrings[kLevelFatal] );</span><br></pre></td></tr></table></figure><p>就行了</p><p>然后从新编译，将so 翻入项目 在看一下现在的效果</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[<span class="number">2020</span>-<span class="number">07</span>-<span class="number">24</span> +<span class="number">8.0</span> <span class="number">11</span>:<span class="number">47</span>:<span class="number">42.597</span>][I]======================&gt;<span class="number">9</span></span><br></pre></td></tr></table></figure><p>ok 打完收工</p><h1 id="简单的封装一下"><a href="#简单的封装一下" class="headerlink" title="简单的封装一下"></a>简单的封装一下</h1><p>基本上分析和实现了我们需要的功能，那么把这部分简单的封装一下</p><p>放上核心的 Builder 源码可在下面自行查看</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.allens.xlog</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> android.content.Context</span><br><span class="line"><span class="keyword">import</span> com.tencent.mars.xlog.Log</span><br><span class="line"><span class="keyword">import</span> com.tencent.mars.xlog.Xlog</span><br><span class="line"></span><br><span class="line">class Builder(context: Context) &#123;</span><br><span class="line"></span><br><span class="line">    companion object &#123;</span><br><span class="line">        <span class="comment">//日志的tag</span></span><br><span class="line">        <span class="keyword">var</span> tag = <span class="string">"log_tag"</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//是否是debug 模式</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">var</span> debug = <span class="keyword">true</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">//是否打印控制台日志</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">var</span> consoleLogOpen = <span class="keyword">true</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">//是否每天一个日志文件</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">var</span> oneFileEveryday = <span class="keyword">true</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//默认的位置</span></span><br><span class="line">    <span class="keyword">private</span> val defCachePath = context.getExternalFilesDir(<span class="keyword">null</span>)?.path + <span class="string">"/mmap"</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// mmap 位置 默认缓存的位置</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">var</span> cachePath = defCachePath</span><br><span class="line"></span><br><span class="line">    <span class="comment">//实际保存的log 位置</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">var</span> logPath = context.getExternalFilesDir(<span class="keyword">null</span>)?.path + <span class="string">"/logDir"</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//文件名称前缀 例如该值为TEST，生成的文件名为：TEST_20170102.xlog</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">var</span> namePreFix = <span class="string">"log"</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//写入文件的模式</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">var</span> model = LogModel.Async</span><br><span class="line"></span><br><span class="line">    <span class="comment">//最大文件大小</span></span><br><span class="line">    <span class="comment">//默认情况下，所有日志每天都写入一个文件。可以通过更改max_file_size将日志分割为多个文件。</span></span><br><span class="line">    <span class="comment">//单个日志文件的最大字节大小，默认为0，表示不分割</span></span><br><span class="line">    <span class="comment">// 最大 当文件不能超过 10M</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">var</span> maxFileSize = <span class="number">0L</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//日志级别</span></span><br><span class="line">    <span class="comment">//debug 版本下建议把控制台日志打开，日志级别设为 Verbose 或者 Debug, release 版本建议把控制台日志关闭，日志级别使用 Info.</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">var</span> logLevel = LogLevel.LEVEL_INFO</span><br><span class="line"></span><br><span class="line">    <span class="comment">//通过 python gen_key.py 获取到的公钥</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">var</span> pubKey = <span class="string">""</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//单个文件最大保留时间 最小 1天 默认时间 10天</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">var</span> maxAliveTime = <span class="number">10</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//缓存的天数  一般情况下填0即可。非0表示会在 _cachedir 目录下存放几天的日志。</span></span><br><span class="line">    <span class="comment">//原来缓存日期的意思是几天后从缓存目录移到日志目录</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">var</span> cacheDays = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">    <span class="function">fun <span class="title">setCachePath</span><span class="params">(cachePath: String)</span>: Builder </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.cachePath = cachePath</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function">fun <span class="title">setLogPath</span><span class="params">(logPath: String)</span>: Builder </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.logPath = logPath</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function">fun <span class="title">setNamePreFix</span><span class="params">(namePreFix: String)</span>: Builder </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.namePreFix = namePreFix</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function">fun <span class="title">setModel</span><span class="params">(model: LogModel)</span>: Builder </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.model = model</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function">fun <span class="title">setPubKey</span><span class="params">(key: String)</span>: Builder </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.pubKey = key</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//原来缓存日期的意思是几天后从缓存目录移到日志目录 默认 0 即可</span></span><br><span class="line">    <span class="comment">//如果想让文件保留多少天 用 [setMaxAliveTime] 方法即可</span></span><br><span class="line">    <span class="comment">//大于 0  的时候 默认会放在缓存的位置上 [cachePath]</span></span><br><span class="line">    <span class="function">fun <span class="title">setCacheDays</span><span class="params">(days: Int)</span>: Builder </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (days &lt; <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">this</span>.cacheDays = <span class="number">0</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">this</span>.cacheDays = days</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function">fun <span class="title">setDebug</span><span class="params">(debug: Boolean)</span>: Builder </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.debug = debug</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function">fun <span class="title">setLogLevel</span><span class="params">(level: LogLevel)</span>: Builder </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.logLevel = level</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function">fun <span class="title">setConsoleLogOpen</span><span class="params">(consoleLogOpen: Boolean)</span>: Builder </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.consoleLogOpen = consoleLogOpen</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function">fun <span class="title">setTag</span><span class="params">(logTag: String)</span>: Builder </span>&#123;</span><br><span class="line">        tag = logTag</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * [isOpen]  true   设置每天一个日志文件</span></span><br><span class="line"><span class="comment">     *           false  那么  [setMaxFileSize] 生效</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function">fun <span class="title">setOneFileEveryday</span><span class="params">(isOpen: Boolean)</span>: Builder </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.oneFileEveryday = isOpen</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function">fun <span class="title">setMaxFileSize</span><span class="params">(maxFileSize: Float)</span>: Builder </span>&#123;</span><br><span class="line">        when &#123;</span><br><span class="line">            maxFileSize &lt; <span class="number">0</span> -&gt; &#123;</span><br><span class="line">                <span class="keyword">this</span>.maxFileSize = <span class="number">0L</span></span><br><span class="line">            &#125;</span><br><span class="line">            maxFileSize &gt; <span class="number">10</span> -&gt; &#123;</span><br><span class="line">                <span class="keyword">this</span>.maxFileSize = (<span class="number">10</span> * <span class="number">1024</span> * <span class="number">1024</span>).toLong()</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> -&gt; &#123;</span><br><span class="line">                <span class="keyword">this</span>.maxFileSize = (maxFileSize * <span class="number">1024</span> * <span class="number">1024</span>).toLong()</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * [day] 设置单个文件的过期时间 默认10天 在程序启动30S 以后会检查过期文件</span></span><br><span class="line"><span class="comment">     *       过期时间依据 当前系统时间 - 文件最后修改时间计算</span></span><br><span class="line"><span class="comment">     *       默认 单个文件保存 10天</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function">fun <span class="title">setMaxAliveTime</span><span class="params">(day: Int)</span>: Builder </span>&#123;</span><br><span class="line">        when &#123;</span><br><span class="line">            day &lt; <span class="number">0</span> -&gt; &#123;</span><br><span class="line">                <span class="keyword">this</span>.maxAliveTime = <span class="number">0</span></span><br><span class="line">            &#125;</span><br><span class="line">            day &gt; <span class="number">10</span> -&gt; &#123;</span><br><span class="line">                <span class="keyword">this</span>.maxAliveTime = <span class="number">10</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> -&gt; &#123;</span><br><span class="line">                <span class="keyword">this</span>.maxAliveTime = day</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function">fun <span class="title">init</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (!debug) &#123;</span><br><span class="line">            <span class="comment">//判断如果是release 就强制使用 异步</span></span><br><span class="line">            model = LogModel.Async</span><br><span class="line">            <span class="comment">//日志级别使用 Info</span></span><br><span class="line">            logLevel = LogLevel.LEVEL_INFO</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (cachePath.isEmpty()) &#123;</span><br><span class="line">            <span class="comment">//cachePath这个参数必传，而且要data下的私有文件目录，例如 /data/data/packagename/files/xlog， mmap文件会放在这个目录，如果传空串，可能会发生 SIGBUS 的crash。</span></span><br><span class="line">            cachePath = defCachePath</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        android.util.Log.i(tag, <span class="string">"Xlog=========================================&gt;"</span>)</span><br><span class="line">        android.util.Log.i(</span><br><span class="line">            tag,</span><br><span class="line">            <span class="string">"info"</span> + <span class="string">"\n"</span></span><br><span class="line">                    + <span class="string">"level:"</span> + logLevel.level + <span class="string">"\n"</span></span><br><span class="line">                    + <span class="string">"model:"</span> + model.model + <span class="string">"\n"</span></span><br><span class="line">                    + <span class="string">"cachePath:"</span> + cachePath + <span class="string">"\n"</span></span><br><span class="line">                    + <span class="string">"logPath:"</span> + logPath + <span class="string">"\n"</span></span><br><span class="line">                    + <span class="string">"namePreFix:"</span> + namePreFix + <span class="string">"\n"</span></span><br><span class="line">                    + <span class="string">"cacheDays:"</span> + cacheDays + <span class="string">"\n"</span></span><br><span class="line">                    + <span class="string">"pubKey:"</span> + pubKey + <span class="string">"\n"</span></span><br><span class="line">                    + <span class="string">"consoleLogOpen:"</span> + consoleLogOpen + <span class="string">"\n"</span></span><br><span class="line">                    + <span class="string">"maxFileSize:"</span> + maxFileSize + <span class="string">"\n"</span></span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">        android.util.Log.i(tag, <span class="string">"Xlog=========================================&lt;"</span>)</span><br><span class="line">        Xlog.setConsoleLogOpen(consoleLogOpen)</span><br><span class="line">        <span class="comment">//每天一个日志文件</span></span><br><span class="line">        <span class="keyword">if</span> (oneFileEveryday) &#123;</span><br><span class="line">            Xlog.setMaxFileSize(<span class="number">0</span>)</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            Xlog.setMaxFileSize(maxFileSize)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        Xlog.setMaxAliveTime((maxAliveTime * <span class="number">24</span> * <span class="number">60</span> * <span class="number">60</span>).toLong())</span><br><span class="line"></span><br><span class="line">        Xlog.appenderOpen(</span><br><span class="line">            logLevel.level,</span><br><span class="line">            model.model,</span><br><span class="line">            cachePath,</span><br><span class="line">            logPath,</span><br><span class="line">            namePreFix,</span><br><span class="line">            cacheDays,</span><br><span class="line">            pubKey</span><br><span class="line">        )</span><br><span class="line">        Log.setLogImp(Xlog())</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h1 id="下载"><a href="#下载" class="headerlink" title="下载"></a>下载</h1><p>Step 1. Add the JitPack repository to your build file<br>Add it in your root build.gradle at the end of repositories:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">allprojects &#123;</span><br><span class="line">		repositories &#123;</span><br><span class="line">			...</span><br><span class="line">			maven &#123; url &#39;https:&#x2F;&#x2F;www.jitpack.io&#39; &#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure><p>Step 2. Add the dependency</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">dependencies &#123;</span><br><span class="line">        implementation &#39;com.github.JiangHaiYang01:XLogHelper:Tag&#39;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>添加 abiFilter</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">android &#123;</span><br><span class="line">    compileSdkVersion 30</span><br><span class="line">    buildToolsVersion &quot;30.0.1&quot;</span><br><span class="line"></span><br><span class="line">    defaultConfig &#123;</span><br><span class="line">        ...</span><br><span class="line">        ndk &#123;</span><br><span class="line">            abiFilter &quot;armeabi-v7a&quot;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><blockquote><p>当前最新版本</p></blockquote><p><a href="https://www.jitpack.io/#JiangHaiYang01/XLogHelper" target="_blank" rel="noopener"><img data-src="https://www.jitpack.io/v/JiangHaiYang01/XLogHelper.svg" alt=""></a></p><h1 id="使用-1"><a href="#使用-1" class="headerlink" title="使用"></a>使用</h1><h2 id="初始化，建议放在-Application-中"><a href="#初始化，建议放在-Application-中" class="headerlink" title="初始化，建议放在 Application 中"></a>初始化，建议放在 Application 中</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">XLogHelper.create(this)</span><br><span class="line">    .setModel(LogModel.Async)</span><br><span class="line">    .setTag(&quot;TAG&quot;)</span><br><span class="line">    .setConsoleLogOpen(true)</span><br><span class="line">    .setLogLevel(LogLevel.LEVEL_INFO)</span><br><span class="line">    .setNamePreFix(&quot;log&quot;)</span><br><span class="line">    .setPubKey(&quot;572d1e2710ae5fbca54c76a382fdd44050b3a675cb2bf39feebe85ef63d947aff0fa4943f1112e8b6af34bebebbaefa1a0aae055d9259b89a1858f7cc9af9df1&quot;)</span><br><span class="line">    .setMaxFileSize(1f)</span><br><span class="line">    .setOneFileEveryday(true)</span><br><span class="line">    .setCacheDays(0)</span><br><span class="line">    .setMaxAliveTime(2)</span><br><span class="line">    .init()</span><br></pre></td></tr></table></figure><p>使用</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">XLogHelper.i(&quot;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; %s&quot;, i)</span><br><span class="line">XLogHelper.e(&quot;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt; %s&quot;, i)</span><br></pre></td></tr></table></figure><h1 id="源码"><a href="#源码" class="headerlink" title="源码"></a>源码</h1><p><a href="https://github.com/JiangHaiYang01/XLogHelper" target="_blank" rel="noopener">GitHub</a><br><a href="https://allens.icu/posts/60625924/#more">博客</a><br><a href="https://juejin.im/post/5f110c786fb9a07e802052cd" target="_blank" rel="noopener">掘金</a></p><h1 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h1><p><a href="https://github.com/Tencent/mars" target="_blank" rel="noopener">Tencent/mars</a><br><a href="https://github.com/Tencent/mars/wiki/Mars-Android-%E6%8E%A5%E5%8F%A3%E8%AF%A6%E7%BB%86%E8%AF%B4%E6%98%8E" target="_blank" rel="noopener">Mars Android 接口详细说明</a><br><a href="https://zhuanlan.zhihu.com/p/23879436" target="_blank" rel="noopener">【腾讯Bugly干货分享】微信mars 的高性能日志模块 xlog</a><br><a href="https://github.com/Tencent/mars/wiki/Mars-Android-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97#local_compile" target="_blank" rel="noopener">本地编译</a></p></div><div class="reward-container"><div></div><button onclick='var e=document.getElementById("qr");e.style.display="none"===e.style.display?"block":"none"'>打赏</button><div id="qr" style="display:none"><div style="display:inline-block"><img src="/images/wechatpay.png" alt="在雨季等你 微信支付"><p>微信支付</p></div><div style="display:inline-block"><img src="/images/alipay.png" alt="在雨季等你 支付宝"><p>支付宝</p></div></div></div><div><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者： </strong>在雨季等你</li><li class="post-copyright-link"><strong>本文链接：</strong> <a href="https://allens.icu/posts/60625924/" title="基于微信XLog的日志框架&amp;&amp;对于XLog的分析">https://allens.icu/posts/60625924/</a></li><li class="post-copyright-license"><strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！</li></ul></div><footer class="post-footer"><div class="post-nav"><div class="post-nav-item"><a href="/posts/67dff628/" rel="prev" title="Android进程保活的前生今世"><i class="fa fa-chevron-left"></i> Android进程保活的前生今世</a></div><div class="post-nav-item"><a href="/posts/45482d84/" rel="next" title="Android 帧动画 浅析">Android 帧动画 浅析 <i class="fa fa-chevron-right"></i></a></div></div></footer></article></div><div class="comments" id="valine-comments"></div><script>window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }</script></div><div class="toggle sidebar-toggle"><span class="toggle-line toggle-line-first"></span> <span class="toggle-line toggle-line-middle"></span> <span class="toggle-line toggle-line-last"></span></div><aside class="sidebar"><div class="sidebar-inner"><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc">文章目录</li><li class="sidebar-nav-overview">站点概览</li></ul><div class="post-toc-wrap sidebar-panel"><div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#xlog-是什么"><span class="nav-number">2.</span> <span class="nav-text">xlog 是什么</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#官网的-sample"><span class="nav-number">3.</span> <span class="nav-text">官网的 sample</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#下载库"><span class="nav-number">3.1.</span> <span class="nav-text">下载库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用"><span class="nav-number">3.2.</span> <span class="nav-text">使用</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#分析各个方法的作用"><span class="nav-number">4.</span> <span class="nav-text">分析各个方法的作用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#appenderOpen-int-level-int-mode-String-cacheDir-String-logDir-String-nameprefix-int-cacheDays-String-pubkey"><span class="nav-number">4.1.</span> <span class="nav-text">appenderOpen(int level, int mode, String cacheDir, String logDir, String nameprefix, int cacheDays, String pubkey)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#level"><span class="nav-number">4.1.1.</span> <span class="nav-text">level</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#mode"><span class="nav-number">4.1.2.</span> <span class="nav-text">mode</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cacheDir-设置缓存目录"><span class="nav-number">4.1.3.</span> <span class="nav-text">cacheDir 设置缓存目录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#logDir-设置写入的文件目录"><span class="nav-number">4.1.4.</span> <span class="nav-text">logDir 设置写入的文件目录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#nameprefix-设置日志文件名的前缀"><span class="nav-number">4.1.5.</span> <span class="nav-text">nameprefix 设置日志文件名的前缀</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cacheDays"><span class="nav-number">4.1.6.</span> <span class="nav-text">cacheDays</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#pubkey-设置加密的-pubkey"><span class="nav-number">4.1.7.</span> <span class="nav-text">pubkey 设置加密的 pubkey</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#setMaxFileSize-设置文件大小"><span class="nav-number">4.2.</span> <span class="nav-text">setMaxFileSize 设置文件大小</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#setConsoleLogOpen-设置是否在控制台答应日志"><span class="nav-number">4.3.</span> <span class="nav-text">setConsoleLogOpen 设置是否在控制台答应日志</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#setErrLogOpen"><span class="nav-number">4.4.</span> <span class="nav-text">setErrLogOpen</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#setMaxAliveTime-设置单个文件最大保留时间"><span class="nav-number">4.5.</span> <span class="nav-text">setMaxAliveTime 设置单个文件最大保留时间</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#appenderClose"><span class="nav-number">4.6.</span> <span class="nav-text">appenderClose</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#appenderFlush"><span class="nav-number">4.7.</span> <span class="nav-text">appenderFlush</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#日志文件的加密"><span class="nav-number">5.</span> <span class="nav-text">日志文件的加密</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#日志文件的解密"><span class="nav-number">6.</span> <span class="nav-text">日志文件的解密</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#下载pyelliptic1"><span class="nav-number">6.1.</span> <span class="nav-text">下载pyelliptic1</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#直接解密脚本"><span class="nav-number">6.2.</span> <span class="nav-text">直接解密脚本</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#修改日志的格式"><span class="nav-number">7.</span> <span class="nav-text">修改日志的格式</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#去掉头文件"><span class="nav-number">7.1.</span> <span class="nav-text">去掉头文件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#修改日志格式"><span class="nav-number">7.2.</span> <span class="nav-text">修改日志格式</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#简单的封装一下"><span class="nav-number">8.</span> <span class="nav-text">简单的封装一下</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#下载"><span class="nav-number">9.</span> <span class="nav-text">下载</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#使用-1"><span class="nav-number">10.</span> <span class="nav-text">使用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#初始化，建议放在-Application-中"><span class="nav-number">10.1.</span> <span class="nav-text">初始化，建议放在 Application 中</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#源码"><span class="nav-number">11.</span> <span class="nav-text">源码</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#参考"><span class="nav-number">12.</span> <span class="nav-text">参考</span></a></li></ol></div></div><div class="site-overview-wrap sidebar-panel"><div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person"><img class="site-author-image" itemprop="image" alt="在雨季等你" src="/images/heard.jpg"><p class="site-author-name" itemprop="name">在雨季等你</p><div class="site-description" itemprop="description">分享是进步最快的方式</div></div><div class="site-state-wrap motion-element"><nav class="site-state"><div class="site-state-item site-state-posts"><a href="/archives/"><span class="site-state-item-count">23</span> <span class="site-state-item-name">日志</span></a></div><div class="site-state-item site-state-categories"><a href="/categories/"><span class="site-state-item-count">7</span> <span class="site-state-item-name">分类</span></a></div><div class="site-state-item site-state-tags"><a href="/tags/"><span class="site-state-item-count">6</span> <span class="site-state-item-name">标签</span></a></div></nav></div><div class="links-of-author motion-element"><span class="links-of-author-item"><a href="https://github.com/JiangHaiYang01" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;JiangHaiYang01" rel="noopener" target="_blank"><i class="github fa-fw"></i>GitHub</a> </span><span class="links-of-author-item"><a href="https://www.jianshu.com/u/142e8a9d9d71" title="简书 → https:&#x2F;&#x2F;www.jianshu.com&#x2F;u&#x2F;142e8a9d9d71" rel="noopener" target="_blank"><i class="heartbeat fa-fw"></i>简书</a> </span><span class="links-of-author-item"><a href="https://juejin.im/user/5ade713ef265da0b92653545/posts" title="掘金 → https:&#x2F;&#x2F;juejin.im&#x2F;user&#x2F;5ade713ef265da0b92653545&#x2F;posts" rel="noopener" target="_blank"><i class="heartbeat fa-fw"></i>掘金</a></span></div><div class="links-of-blogroll motion-element links-of-blogroll-block"><div class="links-of-blogroll-title"><i class="fa fa-history fa-" aria-hidden="true"></i> 近期文章</div><ul class="links-of-blogroll-list"><li><a href="/posts/51fd8696/" title="线程总结" target="_blank">线程总结</a></li><li><a href="/posts/dac154ea/" title="Jetpack之Navigation浅析" target="_blank">Jetpack之Navigation浅析</a></li><li><a href="/posts/6ef6837/" title="DataBinding 浅析" target="_blank">DataBinding 浅析</a></li><li><a href="/posts/1981ac2c/" title="文章总览" target="_blank">文章总览</a></li><li><a href="/posts/f6108cad/" title="反射浅析" target="_blank">反射浅析</a></li></ul></div></div><div class="back-to-top motion-element"><i class="fa fa-arrow-up"></i> <span>0%</span></div></div></aside><div id="sidebar-dimmer"></div></div></main><footer class="footer"><div class="footer-inner"><div class="copyright">&copy; 2018 – <span itemprop="copyrightYear">2020</span> <span class="with-love"><i class="fa fa-heart"></i> </span><span class="author" itemprop="copyrightHolder">在雨季等你</span></div></div></footer></div><script src="/lib/anime.min.js"></script><script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script><script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script><script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script><script src="/lib/velocity/velocity.min.js"></script><script src="/lib/velocity/velocity.ui.min.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/pisces.js"></script><script src="/js/next-boot.js"></script><script>!function(){var e,t,o=document.getElementsByTagName("link");if(o.length>0)for(i=0;i<o.length;i++)"canonical"==o[i].rel.toLowerCase()&&o[i].href&&(e=o[i].href);t=e?e.split(":")[0]:window.location.protocol.split(":")[0],e||(e=window.location.href),!function(){var i=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,o=e,r=document.referrer;if(!i.test(o)){var n="https"===String(t).toLowerCase()?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";r?(n+="?r="+encodeURIComponent(document.referrer),o&&(n+="&l="+o)):o&&(n+="?l="+o);var a=new Image;a.src=n}}(window)}()</script><script src="/js/local-search.js"></script><script src="//cdn.jsdelivr.net/npm/quicklink@1/dist/quicklink.umd.js"></script><script>window.addEventListener('load', () => {
      quicklink({
        timeout : 3000,
        priority: true,
        ignores : [uri => uri.includes('#'),uri => uri === 'https://allens.icu/posts/60625924/',]
      });
      });</script><script>NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : '6lUfyTDNhn0hAsWfRDnGKAV3-gzGzoHsz',
      appKey     : 'EsKctJGDiO5s9Dd5qQ4791DG',
      placeholder: "欢迎吐槽",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});</script></body></html><!-- rebuild by neat -->